//
//  AddFriendViewController.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/14.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "AddFriendViewController.h"

@interface AddFriendViewController ()<UITextFieldDelegate>

@end

@implementation AddFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加好友";
    self.navigationItem.backBarButtonItem.tintColor = [UIColor greenColor];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //添加好友账号
    NSString *userNumber = textField.text;
    if (![SLUtils isNilString:userNumber]) {
        //判断是否是添加自己为好友
        if ([userNumber isEqualToString:[UserLocalInfo sharedUserInfo].userId]) {
            [ProgressHUD showError:@"不能添加自己为好友"];
            return YES;
        }
         XMPPJID *friendJID = [XMPPJID jidWithUser:textField.text domain:kLoginDomain resource:nil];
        //判断是否已经添加了好友
        if ([[XMPPManager sharedManager].rosterStorage userExistsWithJID:friendJID xmppStream:[XMPPManager sharedManager].xmppStream]) {
            [ProgressHUD showError:@"该好友已经存在"];
            return YES;
        }
        SLog(@"add friend: %@", userNumber);
        //发送添加好友请求[花名册订阅]
        [[[XMPPManager sharedManager] roster] subscribePresenceToUser:friendJID];
    }
    else {
        [ProgressHUD showError:@"请输入好友账号"];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
